import pygame
import flame

pygame.init()

class gun_fire(pygame.sprite.Sprite):

    def __init__(self, off_set_x, off_set_y, belong_to):
        super(gun_fire, self).__init__(self.containers)
        self.belong_to = belong_to
        self.off_set_x = off_set_x
        self.off_set_y = off_set_y
        self.image = pygame.image.load("images/gunfire.png")
        #self._image = self.image
        self.rect = self.image.get_rect(center = (self.off_set_x + self.belong_to.rect.center[0], self.off_set_y + self.belong_to.rect.center[1]))

    def draw(self, screen):
        screen.blit(self.image, self.rect)

    def update(self):
        self.rect.center = (self.off_set_x + self.belong_to.rect.center[0], self.off_set_y + self.belong_to.rect.center[1])
        self.kill()

"""
    class shot_gun:

    def __init__(self, offset_x, offset_y, belong_to, speed_x, speed_y, shot_type = 1, fire_intv = 2):
        self.offset_x = offset_x
        self.offset_y = offset_y
        self.belong_to = belong_to
        self.speed_x = speed_x
        self.speed_y = speed_y
        self.shot_type = shot_type
        self.FIRE_INTV = fire_intv
        self.fire_intv = FIRE_INTV

    def triger(self):
        pass
"""

class shot_obj(pygame.sprite.Sprite):

    def __init__(self, screen_size, xspeed, yspeed, postion, belong_to = None, laser_type = 1, target = None, angle = 0):

        super(shot_obj, self).__init__(self.containers)
        self.screen_width = screen_size[0]
        self.screen_height = screen_size[1]
        self.xspeed = xspeed
        self.yspeed = yspeed
        self.angle = angle
        self.belong_to = belong_to
        self.target = target

        if (laser_type == 1):
            self.image = pygame.image.load("images/laser.png")
            #self._image = self.image                        #why need '_' ?
            self.rect = self.image.get_rect(center = postion)   #set the rect's pos when get it
            self.x, self.y = self.rect.center
            self.power = 1

        elif (laser_type == 2):
            self.image = pygame.image.load("images/laser1.png")
            #self._image = self.image                        #why need '_' ?
            self.rect = self.image.get_rect(center = postion)   #set the rect's pos when get it
            self.x, self.y = self.rect.center
            self.power = 2

#        self.rotate()

    def draw(self, screen):
        screen.blit(self.image, self.rect)

    def update(self):
        self.rect.center = self.x, self.y
        self.x += self.xspeed
        self.y += self.yspeed

        if (self.rect.top >= self.screen_height):
            self.kill()
        if (self.rect.bottom <= 0):
            self.kill()
        if (self.rect.right <= 0):
            self.kill()
        if (self.rect.left >= self.screen_width):
            self.kill()

    def rotate(self):

        center = self.rect.center
        self.image = pygame.transform.rotozoom(self.image, self.angle, 1.0)
        self.rect = self.image.get_rect()
        self.rect.center = center
        
class enemy_shot_obj(pygame.sprite.Sprite):

    def __init__(self, screen_size, xspeed, yspeed, postion, belong_to = None, laser_type = 1, target = None, angle = 0):

        super(enemy_shot_obj, self).__init__(self.containers)
        self.screen_width = screen_size[0]
        self.screen_height = screen_size[1]
        self.xspeed = xspeed
        self.yspeed = yspeed
        self.angle = angle
        self.belong_to = belong_to
        self.target = target

        if (laser_type == 1):
            self.image = pygame.image.load("images/efire1.png")
            #self._image = self.image                        #why need '_' ?
            self.rect = self.image.get_rect(center = postion)   #set the rect's pos when get it
            self.x, self.y = self.rect.center
            self.power = 1

#        self.rotate()

    def draw(self, screen):
        screen.blit(self.image, self.rect)

    def update(self):
        self.rect.center = self.x, self.y
        self.x += self.xspeed
        self.y += self.yspeed

        if (self.rect.top >= self.screen_height):
            self.kill()
        if (self.rect.bottom <= 0):
            self.kill()
        if (self.rect.right <= 0):
            self.kill()
        if (self.rect.left >= self.screen_width):
            self.kill()

    def rotate(self):

        center = self.rect.center
        self.image = pygame.transform.rotozoom(self.image, self.angle, 1.0)
        self.rect = self.image.get_rect()
        self.rect.center = center

class bomb_obj(pygame.sprite.Sprite):

    def __init__(self, screen_size, postion, belong_to = None, bomb_type = 1, target = None, angle = 0):
        super(bomb_obj, self).__init__(self.containers)
        self.screen_width = screen_size[0]
        self.screen_height = screen_size[1]
        self.angle = angle
        self.belong_to = belong_to
        self.target = target
        self.move_step = 0
        self.kill_R = 0
        self.bomb_last_time = 0
        self.isbombed = False

        if (bomb_type == 1):
            self.image = pygame.image.load("images/bomb1.png")
            #self._image = self.image                        #why need '_' ?
            self.rect = self.image.get_rect(center = postion)   #set the rect's pos when get it
            self.x, self.y = self.rect.center
            self.kill_R = 150
            self.move_step = 3
            self.bomb_last_time = 10
            self.speed = 15
            self.power = 100

        else:
            pass

    def draw(self, screen):
#        if (not self.isbombed):
        screen.blit(self.image, self.rect)

    def bomb(self):
        if (self.isbombed):
            return

        self.image = pygame.Surface((self.kill_R * 2, self.kill_R * 2))
        pygame.draw.ellipse(self.image, (255, 255, 255), [0,0,self.kill_R * 2,self.kill_R * 2])
        pygame.draw.ellipse(self.image, (0, 0, 0), [1,1,self.kill_R * 2 - 2,self.kill_R * 2 - 2])
        self.image.set_colorkey((0,0,0), pygame.RLEACCEL)
        self.image.convert()
        self.rect = self.image.get_rect(center = self.rect.center)
        
        #flames!!!
#        flame.explosion(self.rect.center, 60)
        self.isbombed = True

    def bombed(self):
        return self.isbombed

    def update(self):
        if (self.move_step):
            self.move_step -= 1
            self.rect = self.rect.move(0, -self.speed)
        else:
            self.bomb()

        if (self.isbombed):
            if (self.bomb_last_time):
                self.bomb_last_time -= 1
            else:
                self.kill()
